/*
 * @(#)StringUtil.java	1.0 Mar 7, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.util;

import java.util.*;

public class StringUtil
{
	public static String[] subset(String[] strings, int startInclusive)
	{
		return subset(strings, startInclusive, strings.length - 1);
	}

	public static String[] subset(String[] strings, int startInclusive, int endInclusive)
	{
		int length = endInclusive - startInclusive + 1;
		String[] copy = new String[length];
		int copyPos = 0;
		for(int origPos=startInclusive; origPos<=endInclusive; origPos++)
			copy[(copyPos++)] = strings[origPos];

		return copy;
	}

	public static String join(String[] strings, String joiner)
	{
		StringBuilder sb = new StringBuilder();
		boolean firstTime = true;
		for(String s : strings)
		{
			if(!firstTime)
				sb.append(joiner);
			else
				firstTime = false;

			sb.append(s);
		}
		return sb.toString();
	}

	public static String join(Set<String> strings, String joiner)
	{
		StringBuilder sb = new StringBuilder();
		boolean firstTime = true;
		for(String s : strings)
		{
			if(!firstTime)
				sb.append(joiner);
			else
				firstTime = false;

			sb.append(s);
		}
		return sb.toString();
	}

	public static String join(List<String> strings, String joiner)
	{
		StringBuilder sb = new StringBuilder();
		boolean firstTime = true;
		for(String s : strings)
		{
			if(!firstTime)
				sb.append(joiner);
			else
				firstTime = false;

			sb.append(s);
		}
		return sb.toString();
	}

	// getAllLeadingsOf(getAllEndingsOf)
	public static Set<String> getAllSubstringsOf(String toCheck)
	{
		HashSet<String> all = new HashSet<String>();
		Set<String> ends = getAllEndingsOf(toCheck);
		all.addAll(ends);
		for(String toFind:ends)
			all.addAll(getAllLeadingsOf(toFind));

		return all;
	}

	// all substring endings: friend, riend, iend, end, nd, d
	public static Set<String> getAllEndingsOf(String toCheck)
	{
		HashSet<String> ends = new HashSet<String>();
		int end = toCheck.length();
		for(int i=0; i<=end; i++)
			ends.add(toCheck.substring(i,end));

		return ends;
	}

	// all substring leadings: friend, frien, frie, fri, fr, f
	public static Set<String> getAllLeadingsOf(String toCheck)
	{
		HashSet<String> leads = new HashSet<String>();
		int end = toCheck.length();
		for(int i=end; i>=0; i--)
			leads.add(toCheck.substring(0,i));

		return leads;
	}

	public static List<String> multiline(String source, int width)
	{
		List<String> newlines = Arrays.asList(source.split("\\s"));
		ArrayList<String> lines = new ArrayList<String>(newlines.size());
		for(String newline : newlines)
		{
			if(newline == null)
			{
				lines.add(null);
				continue;
			}

			while(newline.length() > width)
			{
				lines.add(newline.substring(0, width));
				newline = newline.substring(width);
			}

			if(newline.length() > 0)
				lines.add(newline);
		}

		return lines;
	}
}
